/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chating;

import java.util.ArrayList;
import chating.User;
import chating.Message;

/**
 *
 * @author ghufron
 */
public class Room {

    public class Member {
        // attributes
        private String userName;
        private long waktuAwalJoin;

        // constructor
        public Member(){}
        public Member(String userName,long waktuAwalJoin){
            this.userName = userName;
            this.waktuAwalJoin = waktuAwalJoin;
        }
        
        // getter and setter
        /**
         * @return the userName
         */
        public String getUserName() {
            return userName;
        }

        /**
         * @param userName the userName to set
         */
        public void setUserName(String userName) {
            this.userName = userName;
        }

        /**
         * @return the waktuAwalJoin
         */
        public long getWaktuAwalJoin() {
            return waktuAwalJoin;
        }

        /**
         * @param waktuAwalJoin the waktuAwalJoin to set
         */
        public void setWaktuAwalJoin(long waktuAwalJoin) {
            this.waktuAwalJoin = waktuAwalJoin;
        }
    }

    // attributes
    private String nameRoom;
    private String category;
    private String subCategory;
    private String owner;
    private boolean persistence;
    private ArrayList<Member> members;
    private ArrayList<Message> messages;

    // constructor and copy constructor
    public Room(){
        this.members = new ArrayList<Member>();
        this.messages = new ArrayList<Message>();
    }
    public Room(String nama, String kategori, String subKategori, String owner, boolean Persist) {
        this.nameRoom = nama;
        this.category = kategori;
        this.subCategory = subKategori;
        this.owner = owner;
        this.persistence = Persist;
        this.members = new ArrayList<Member>();
        this.messages = new ArrayList<Message>();
    }
    public Room(Room r){
        this.nameRoom = r.getNameRoom();
        this.category = r.getCategory();
        this.subCategory = r.getSubCategory();
        this.owner = r.getOwner();
        this.persistence = r.isPersistence();
        this.members = new ArrayList<Member>(r.getMembers());
        this.messages = new ArrayList<Message>(r.getMessages());
    }

    // getter and setter
    /**
     * @return the nameRoom
     */
    public String getNameRoom() {
        return nameRoom;
    }

    /**
     * @param nameRoom the nameRoom to set
     */
    public void setNameRoom(String nameRoom) {
        this.nameRoom = nameRoom;
    }

    /**
     * @return the category
     */
    public String getCategory() {
        return category;
    }

    /**
     * @param category the category to set
     */
    public void setCategory(String category) {
        this.category = category;
    }

    /**
     * @return the subCategory
     */
    public String getSubCategory() {
        return subCategory;
    }

    /**
     * @param subCategory the subCategory to set
     */
    public void setSubCategory(String subCategory) {
        this.subCategory = subCategory;
    }

    /**
     * @return the owner
     */
    public String getOwner() {
        return owner;
    }

    /**
     * @param owner the owner to set
     */
    public void setOwner(String owner) {
        this.owner = owner;
    }

    /**
     * @return the persistence
     */
    public boolean isPersistence() {
        return persistence;
    }

    /**
     * @param persistence the persistence to set
     */
    public void setPersistence(boolean persistence) {
        this.persistence = persistence;
    }

    /**
     * @return the members
     */
    public ArrayList<Member> getMembers() {
        return members;
    }

    /**
     * @param members the members to set
     */
    public void setMembers(ArrayList<Member> members) {
        this.members = members;
    }

    /**
     * @return the messages
     */
    public ArrayList<Message> getMessages() {
        return messages;
    }

    /**
     * @param messages the messages to set
     */
    public void setMessages(ArrayList<Message> messages) {
        this.messages = messages;
    }
    
    // methods
    public void addMember(String userName){
        Member member = new Member();
        member.setUserName(userName);
        long timeNow = System.currentTimeMillis();
        member.setWaktuAwalJoin(timeNow);
        getMembers().add(member);
    }

    public void removeMember(String userName){
        for (int i=0; i<this.members.size(); i++){
            if (this.members.get(i).getUserName().equalsIgnoreCase(userName)){
                this.members.remove(i);
                break;
            }
        }
    }

    public void addMessage(String namePost, String contentPost){
        Message m = new Message(namePost, contentPost);
        this.messages.add(m);
    }

    public ArrayList<Message> getLastMessages(int numMessages){
        ArrayList<Message> m = new ArrayList<Message>();
        int n=0;
        for(int i=0; i<this.messages.size(); i++){
            if (i+numMessages > this.messages.size()){
                m.add(this.messages.get(i));
            }
        }
        return m;
    }

    public String getInfoRoom(DataContainer cont, String roomName){
        Room r= new Room(cont.getRoomByName(roomName));
        String s = "Nama room= " + r.getNameRoom() + "\nKategori = " + r.getCategory() +"\nSub kategori = " + r.getSubCategory() + "\nRoom owner = " + r.getOwner() + "\nPersistence = " + r.isPersistence() + "\n";
        return s;
    }

    public String getUserWithLongTime(){
        String s;
        int indexMax=0;
        for(int i=1; i<this.getMembers().size(); i++){
            if(this.getMembers().get(indexMax).getWaktuAwalJoin() > this.getMembers().get(i).getWaktuAwalJoin()){
                indexMax = i;
            }
        }
        s = this.getMembers().get(indexMax).getUserName();
        return s;
    }

    public void changeOwnerOnNonPersistRoom(){
        
    }

    


}
